﻿using System;
using System.Xml;
using System.IO;

using DreamCube.Foundation.Basic.Utility;

namespace DreamCube.Websites.UI.Projects.YXGL
{
    public partial class UpgradeCheck : System.Web.UI.Page
    {
        /// <summary>
        /// 返回给客户端的XML格式
        /// </summary>
        private static String resultConfigXml = "<?xml version='1.0' encoding='utf-8' ?>" +
                                                "<results>" +
                                                  "<updateFileName>{0}</updateFileName>" +
                                                  "<updateFileUrl>{1}</updateFileUrl>" +
                                                  "<fileSize>{2}</fileSize>" +
                                                  "<version>{3}</version>" +
                                                "</results>";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                String ver = Request.QueryString["ver"];
                String platform = Request.QueryString["platform"];
                //post过来的版本号
                Double verNumber = 0.0;

                //platform：0是IOS, 1是Android, 2是IDE(无升级机制),3是symbian(目前暂不支持升级)
                if (!String.IsNullOrEmpty(ver) && !String.IsNullOrEmpty(platform) && Double.TryParse(ver, out verNumber))
                {
                    XmlDocument doc = GetCurVersionConfig();
                    String parentXPath = String.Format("/CurVersion/platform[@type={0}]", platform);
                    String curVersion = MyXml.GetInnerText(doc, String.Format("{0}/version", parentXPath));
                    Double curVersionNumber = 0.0;
                    if (Double.TryParse(curVersion, out curVersionNumber) && curVersionNumber > verNumber)
                    {
                        String fileName = MyXml.GetInnerText(doc, String.Format("{0}/updateFileUrl", parentXPath));
                        String filePath = MyWebsite.GetServerAddr() + "Projects/YXGL/UpdateFiles/";
                        String fileSize = File.ReadAllBytes(Path.Combine(MyWebsite.MapPath(filePath), fileName)).Length.ToString();
                        String resultXml = String.Format(resultConfigXml,
                                                         MyXml.GetInnerText(doc, "/CurVersion/updateFileName"),
                                                         filePath + fileName,
                                                         fileSize,
                                                         MyXml.GetInnerText(doc, String.Format("{0}/version", parentXPath)));
                        Response.ContentType = "text/xml";
                        Response.Write(resultXml);
                    }
                }
                Response.Write("");
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
            finally
            {
                Response.End();
            }
        }

        /// <summary>
        /// 获取当前版本的配置信息
        /// </summary>
        /// <returns></returns>
        protected XmlDocument GetCurVersionConfig()
        {
            try
            {
                String configPath = MyWebsite.GetServerAddr() + "Projects/YXGL/CurVersion.xml";
                XmlDocument doc = new XmlDocument();
                doc.Load(configPath);
                return doc;
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
            return null;
        }
    }
}